package com.googlecode.grs.deploy;

import javax.swing.JFrame;

import com.googlecode.grs.core.GRTRobot;
import com.googlecode.grs.environment.SimulationEnvironment;
import com.googlecode.grs.environment.SimulationEnvironmentFactory;
import com.googlecode.grs.robot.DifferentialDrivenBot;
import com.googlecode.grs.robot.DrawnRobot;
import com.googlecode.grs.robot.RobotFactory;
import com.googlecode.grs.sensor.BeaconSensor;
import com.googlecode.grs.sensor.RangeFinder;
import com.googlecode.grs.sensor.Servo;
import com.googlecode.grs.ui.SimulatorUI;

public class Main extends GRTRobot {

	public Main() {
		super();
	}

	public void robotInit() {
		log("initializing...");

		SimulationEnvironment env = SimulationEnvironmentFactory
				.tryConfiguration("maps/Example.maze");

		DrawnRobot drawnRobot = RobotFactory.getDrawnDifferential(env,
				"maps/Example.maze");
		DifferentialDrivenBot robot = (DifferentialDrivenBot) drawnRobot
				.getRobot();

		Servo sweeper = new Servo(0);
		// initialize components (and abstract them)
		final int NUM_SENSORS = 6;
		RangeFinder[] sonars = new RangeFinder[NUM_SENSORS];
		int index = 0;
		for (int i = -60; i <= 60; i += 20) {
			if (i == 0) {
				continue;
			}
			sonars[index] = new RangeFinder(env, sweeper, 0, 0,
					Math.toRadians(i));
			sonars[index++].start();
		}

		BeaconSensor beaconsensor = new BeaconSensor(env);
		for (RangeFinder sonar : sonars) {
			drawnRobot.addRobotComponent(sonar);
		}
		drawnRobot.addRobotComponent(beaconsensor);

		// TODO is there a way to "factorize" this?
		SimulatorUI sim = new SimulatorUI();
		sim.addDrawable(env);
		sim.addDrawable(drawnRobot);

		JFrame home = new JFrame("grs: robot simulator");
		home.getContentPane().add(sim);
		home.setLocation(200, 200);
		home.setSize(1000, 800);
		home.setVisible(true);
		home.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		sim.startPaintLoop();

		robot.drive(160, 145);

	}

}
